﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Data;
using System;

public class SellerLoginSystem : MonoBehaviour {
	public int num,t,n;
    public string url = "http://localhost/sell/login.php";
	public string url_hb = "http://lzgfds.cn:8080/tqylf/0testhb.php";
	public string url_xg = "http://gongfudashi.cn:8080/sell/0testxg.php";
    public InputField inputName;
    public InputField inputPsw;
    public Toggle isSave;
    public Text tipInfo;
    public string menuName;
    public string s1;
    public string s6;
    int save,kk;
	float tt;
    string _name, _psw, username,psw,data,get;

    void Start()
    {
		n = 0;
		save = PlayerPrefs.GetInt("save", 0);
        if (save == 1)
        {
            isSave.isOn = true;
            _name = PlayerPrefs.GetString("name");
            _psw = PlayerPrefs.GetString("psw");
            //Debug.Log(_name + "  " + _psw);
            inputName.text = _name;
            inputPsw.text = _psw;
        }
        else
        {
            isSave.isOn = false;
        }
    }
		
    public void TestHb()
    {
		Jump();
		url = url_hb;
		InvokeRepeating("Test",0,tt);
    }

	public void TestXg()
	{
		Jump();
		url = url_xg;
		InvokeRepeating("Test",0,tt);
	}
	void Jump()
	{
		n = 0;
		kk = 0;
		username = inputName.text;
		psw = inputPsw.text;
		num = int.Parse(username);
		t = int.Parse(psw);
		tt = t * 0.001f;
		if (isSave.isOn)
		{
			PlayerPrefs.SetInt("save", 1);
			PlayerPrefs.SetString("name", inputName.text);
			PlayerPrefs.SetString("psw", inputPsw.text);
		}
		else
		{
			PlayerPrefs.SetInt("save", 0);
			PlayerPrefs.SetString("name", null);
			PlayerPrefs.SetString("psw", null);
		}
	}
	public void TestStop(){
		n = num;
	}

	public void Test()
	{
		if (n < num) 
		{
			n++;
			StartCoroutine(SentMessage());
		}
		else 
		{
			CancelInvoke();
			ShowTip("测试结束！发送次数 n="+n+"成功接收次数 k="+kk);
		}
	}
    //利用协同程序 发送数据到PHP服务器，并接收和处理服务器返回的数据
    IEnumerator SentMessage()
    {
        #region
		WWWForm form = new WWWForm();
		form.AddField("action", n);
		form.AddField("velue", kk);
		form.AddField("uid", n);
		get ="?name=test"+n;
		WWW www = new WWW(url+get, form);
        #endregion
        #region 接收和处理服务器端返回的单个字符串数据
        yield return www;
        if (www.error != null)
        {
            ShowTip("服务器连接失败！请检查网络是否通畅！");
            Debug.LogError(www.error);
        }
        else
		{
			kk++;
			data = "";
			data = www.text;
			Debug.Log("成功！n="+n+" k="+kk+" data：" + data);
			ShowTip("成功！n="+n+" k="+kk+" data：" + data);
			if(n == num) 
			{
				ShowTip("测试结束!已发送次数 n="+n+" 已成功接收次数 k="+kk);
			}
        }
        #endregion
    }


    void SetPsw()
    {
        inputPsw.text = "";
    }

    void ShowTip(string ss)
    {
        tipInfo.text = ss;
    }
    
}
